package com.demo.quartz;

import com.demo.controller.UpdateRecordUtils;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by tianzhen.wtz on 2015/4/8 0008.
 *
 */
public class SyncRecordJob implements Job{


    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("job start...");

        ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat("SyncRecordJob-Thread-%d")
                .setDaemon(true)
                .build();
        BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(10);
        ExecutorService syncRecodeRunnablePool = new ThreadPoolExecutor(10, 12,0L,TimeUnit.MILLISECONDS,queue,threadFactory);

        int start=0;
        int step=1;
        String cxrq ;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        cxrq = sdf.format(new Date());
        List<Record> records = Db.find("select * from t_car limit " + step + " offset " + start);
        while ((null!=records)&&!records.isEmpty()){
            System.out.println("current block is "+start);

            SyncRecodeRunnable runnable=new SyncRecodeRunnable(records,cxrq);
            try {
                syncRecodeRunnablePool.execute(runnable);
                start=start+step;
                records = Db.find("select * from t_car limit " + step + " offset " + start);
            } catch (RejectedExecutionException e){
                System.out.println("SyncRecordJob-RejectedExecutionException...need wait");
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException ignored) {
                }
            } catch (Exception e) {
                System.out.println("SyncRecordJob-Exception..."+e.getMessage());
            }
        }

    }

    /**
     * ִ��������߳�
     */
    class SyncRecodeRunnable implements Runnable{

        List<Record> records;

        String cxrq;

        public SyncRecodeRunnable(List<Record> records, String cxrq){
            this.records=records;
            this.cxrq=cxrq;
        }

        public void run() {
            for (Record record : records) {
                String hphm = record.getStr("hphm");
                String engineno = record.getStr("engine_num");
                String classno = record.getStr("body_num");
                String openid = record.getStr("openid");

                if (null==engineno||"".equals(engineno)){
                    continue;
                }
                if (null==classno||"".equals(classno)){
                    continue;
                }
                List<Record> cityList = Db
                        .find("SELECT DISTINCT(city_name) as city FROM `t_xian` WHERE abbr='" + hphm.substring(0, 1) + "'");
                for (Record record2 : cityList) {//�������
                    try {
                        System.out.println(">>>select " + record2.get("city"));
                        String queryStr = "cityName=" +java.net.URLEncoder.encode(String.valueOf(record2.get("city")), "utf-8")+ "&plateNumber="
                                + java.net.URLEncoder.encode(hphm, "utf-8") + "&hpzl="
                                + "02" + "&engineNumber=" + engineno + "&vehicleIdNumber=" + classno
                                + "&key=" + UpdateRecordUtils.carkey;
                        //1 ����
                        UpdateRecordUtils.updateRecord(queryStr, hphm, cxrq,"1",openid);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }
        }


    }

}
